package com.shutong.edu.invoke.retry.interceptor;

import com.shutong.edu.invoke.retry.model.RetryInfo;
import com.shutong.edu.invoke.retry.processor.CglibRetryProcessor;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 *
 * @ClassName: RetryCglibInterceptor.java
 * @author: Naughty Guo
 * @date: Jun 2, 2016
 */
public class RetryCglibInterceptor implements MethodInterceptor {

    private Object targetOriginalObject;

    public RetryCglibInterceptor(Object targetOriginalObject) {
        this.targetOriginalObject = targetOriginalObject;
    }

    @Override
    public Object intercept(Object object, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        RetryInfo retryInfo = new RetryInfo();
        retryInfo.setTargetMethod(method);
        retryInfo.setTargetProxyObject(object);
        retryInfo.setTargetOriginalObject(targetOriginalObject);
        retryInfo.setMethodProxy(methodProxy);
        retryInfo.setArguments(objects);
        return new CglibRetryProcessor().retryProcess(retryInfo);
    }
}
